package com.itheima.day06.extra2;


import java.util.Scanner;

public class Used extends State<Activity> {
    Activity activity;

    public Used(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void longin() {

    }

    @Override
    public void saveMoney() {
        System.out.println("欢迎存钱！");
        Scanner scanner = new Scanner(System.in);
        int ge;
        int shi;
        int num;
        try {
            do {
                System.out.println("please input your number 100-1000");
                num = Integer.parseInt(scanner.nextLine());
                ge = num % 10;
                shi = num / 10 % 10;
            } while (!(ge == 0 && shi == 0 && num <= 1000 && num >= 100));
            activity.cash += num;
            System.out.println("存钱成功！余额还有：" + activity.cash);
            int select = 0;
            do {
                System.out.println("继续存钱请输入1，离开输入2，进行银行其他操作输入3");
                select = Integer.parseInt(scanner.nextLine());
            } while (select != 2 && select != 1 && select != 3);
            switch (select) {
                case 1:
                    saveMoney();
                    break;
                case 2:
                    activity.state = activity.logOutState;
                    activity.state.init();
                    break;
                case 3:
                    init();
                    break;
                default:
                    saveMoney();
                    break;
            }
        } catch (Exception e) {
            System.out.println("系统错误退出！");
        }
    }

    @Override
    public void loadMoney() {
        System.out.println("欢迎取钱！");
        Scanner scanner = new Scanner(System.in);
        int ge;
        int shi;
        int num;
        try {
            do {
                System.out.println("please input your number 100-1000");
                num = Integer.parseInt(scanner.nextLine());
                ge = num % 10;
                shi = num / 10 % 10;
            } while (!(ge == 0 && shi == 0 && num <= 1000 && num >= 100));
            if (activity.cash > num) {
                activity.cash -= num;
                System.out.println("取钱成功！余额还有：" + activity.cash);
            } else {
                System.out.println("余额不够！");
            }
            int select = 0;
            do {
                System.out.println("继续取钱请输入1，离开输入2，进行银行其他操作输入3");
                select = Integer.parseInt(scanner.nextLine());
            } while (select != 2 && select != 1 && select != 3);
            switch (select) {
                case 1:
                    loadMoney();
                    break;
                case 2:
                    activity.state = activity.logOutState;
                    activity.state.init();
                    break;
                case 3:
                    init();
                    break;
                default:
                    loadMoney();
                    break;
            }
        } catch (Exception e) {
            System.out.println("系统错误退出！");
        }

    }

    @Override
    public void init() {
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.println("欢迎使用ATM，输入1 代表取钱输入2代表存钱！");
            int select = -1;
            do {
                select = Integer.parseInt(scanner.nextLine());
            } while (!(select == 1 || select == 2));
            switch (select) {
                case 1:
                    loadMoney();
                    break;
                case 2:
                    saveMoney();
                    break;
                default:
                    init();
                    break;
            }
        } catch (Exception e) {
            System.out.println("系统错误退出！");
        }

    }
}
